﻿using PowerHub.MES.DTO.Crew.QueryDto;
using PowerHub.MES.EFBase;
using PowerHub.MES.Entities.Crew;
using PowerHub.MES.Entities;
using PowerHub.MES.IRepository.ICrew;
using PowerHub.MES.ReturnEntity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using PowerHub.MES.Entities.ProductPlanning;
using PowerHub.MES.IRepository.IProductPlanning;
using Microsoft.EntityFrameworkCore;
using PowerHub.MES.DTO.ProductPlanning.QueryDTO;
using PowerHub.MES.Common.Utility;

namespace PowerHub.MES.Repository.ProductPlanning
{

    /// <summary>
    ///产品BOM数据实现层
    /// </summary>
    public class ProductPlanning_ProductBillDetialRepository : BaseRepository<ProductPlanning_ProductBillDetial>, IProductPlanning_ProductBillDetialRepository
    {
        private PowerHubMesContext _repositorySys;
        public ProductPlanning_ProductBillDetialRepository(PowerHubMesContext repositorySys) : base(repositorySys)
        {
            _repositorySys = repositorySys;
        }


        /// <summary>
        /// 添加产品BOM
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<OperationResult<bool>> CreateProductPlanning_ProductBillDetialAsync(List<ProductPlanning_ProductBillDetial> query)
        {
            var result = new OperationResult<bool>();
            result.Data = false;
            if (AddEntitieRange(query) > 0)
            {
                result.Data = true;
            }
            return result;
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<OperationResult<bool>> DeletionProductPlanning_ProductBillDetialAsync(List<string>? ids)
        {
            var result = new OperationResult<bool>();

            var Departments = GetEntitys().AsNoTracking().Where(p => ids.Contains(p.Id)).ToList();
            Departments.ForEach(w => w.IsDel = 1);
            result.Data = UpdataEntityRange(Departments) > 0 ? true : false;

            return result;
        }

        /// <summary>
        /// 根据id获取原料列表
        /// </summary>
        /// <param name = "query" ></ param >
        /// < returns ></ returns >
        public async Task<List<ProductPlanning_ProductBillDetial>> GetProductPlanning_ProductBillDetialListAsync(List<string>? ids, string? ProductBillId)
        {
            return await GetEntitys().AsNoTracking()
                 .Where(p => ids.Contains(p.MaterialsId))
                 .Where(p => p.IsDel == 0)
                 .WhereIf(!string.IsNullOrEmpty(ProductBillId), p => p.ProductBillId.Equals(ProductBillId))
                 .ToListAsync();
        }


        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<OperationResult<bool>> UpdateProductPlanning_ProductBillDetialAsync(ProductPlanning_ProductBillDetial query)
        {
            var result = new OperationResult<bool>();
            result.Data = false;
            if (UpdataEntity(query) > 0)
            {
                result.Data = true;
            }
            return result;
        }
    }
}
